/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package locusts.server.collisions;

import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import locusts.common.Player;
import locusts.common.entities.Entity;
import locusts.common.entities.InputEntity;

public class DamageLocustCR implements CollisionResponce {

    public DamageLocustCR() {
    }

    public void doCollision(Collidable locustCollidable,
            Collidable touchCollidable, double period) {
        Entity locust = null;
        if (locustCollidable instanceof CollidableEntityAdapter) {
            locust = ((CollidableEntityAdapter) locustCollidable).getEntity();
        } else if (locustCollidable instanceof Entity) {
            locust = ((Entity) locustCollidable);
        } else {
            throw new IllegalArgumentException(
                    "The Collidable arguments to not represent known entities.");
        }
        Entity touch = null;
        if (touchCollidable instanceof CollidableEntityAdapter) {
            touch = ((CollidableEntityAdapter) touchCollidable).getEntity();
        } else if (touchCollidable instanceof Entity) {
            touch = ((Entity) touchCollidable);
        } else {
            throw new IllegalArgumentException(
                    "The Collidable arguments to not represent known entities.");
        }



        if (!locust.isAlive() || !touch.isAlive())
            return;

        // the amount of damage is inversely proptional to the area of the 2 entities.
        
        final double damageBase = 100;

        final Area locustArea = locustCollidable.getArea();
        final Area touchArea = touchCollidable.getArea();

        final Rectangle2D locustBounds = locustArea.getBounds2D();
        final Rectangle2D touchBounds = touchArea.getBounds2D();

        final double locustSize = locustBounds.getWidth() * locustBounds.
                getHeight();
        final double touchSize = touchBounds.getWidth() * touchBounds.
                getHeight();

        final double damageMultiplier = locustSize / touchSize;

        final double damage = damageBase * damageMultiplier;


        locust.setEnergy(locust.getEnergy() - damage);
        locust.setModified();


        // give the player score realive to the damage done.

        if (touch instanceof InputEntity) {
            final InputEntity input = ((InputEntity) touch);
            Player player = input.getPlayer();
            player.addDamage(damage);

            // XXX: This is a total hack because itignores the minDamage
            // property of this entity type.
            if(locust.getEnergy() <= 0)
                player.addKills(1);
//            player.setModified(true);
        }

    }
}
